package com.bci.pwtz.controller.app;

import javax.servlet.http.HttpServletRequest;

import com.bci.pwtz.common.util.PwtzUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.DeviceDictConstants;
import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.remote.ListResponse;
import com.bci.pwtz.common.remote.ObjResponse;
import com.bci.pwtz.common.remote.ReturnCode;
import com.bci.pwtz.common.remote.req.DygtReq;
import com.bci.pwtz.common.remote.req.DygtIdReq;
import com.bci.pwtz.common.remote.req.DygtListReq;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.controller.AbstractController;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Dygt;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.DictionaryAuditService;
import com.bci.pwtz.service.DygtService;
@Controller
@RequestMapping("/api/dygt")
public class DygtAppController extends AbstractController{
@Autowired
DygtService service;//注解注入服务
@Autowired
DictionaryAuditService dictionaryAuditService;
@Autowired
private CompanyService  companyService;
/**
 * app端请求获取设备列表方法
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:24:10<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public ListResponse<Dygt> getDygtList(HttpServletRequest request){
	DygtListReq req=null;
	PaginationList<Dygt> pagination=null;
	ListResponse<Dygt> resp=new ListResponse<Dygt>();
	try{
		req=getJsonToObj(request,DygtListReq.class);
		if(req==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("请求为空错误");
			return resp;
		}
		if(req.getCompanyId()==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(req.getSecretKey()==null){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥为空");
			return resp;
		}
		if(req.getTimestamp()==null){
			resp.setReturnCode(ReturnCode.OTHER_ERROR);
			resp.setReturnMsg("时间戳为空");
			return resp;
		}
		if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥错误");
			return resp;
		}
		String beginTime=req.getBeginTime();
		Long companyId=req.getCompanyId();
		String towername=req.getTowerName();
		pagination=service.selectDygts(towername,companyId,PwtzConstants.PORTAL_TYPE_APP,req.getMedLineCodes(),null,beginTime, null, req.getPageSize(),req.getPage(),null);
		if(null !=pagination){
			resp.setReturnCode(ReturnCode.SUCCESS);
			resp.setReturnMsg("查询杆塔设备成功");
			resp.setObjs(pagination);
			resp.setTimestamp(req.getTimestamp());
		}else{
			resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
			resp.setReturnMsg("查询杆塔设备调用接口失败");
			resp.setTimestamp(req.getTimestamp());
		}
	}catch(PwtzDatabaseException e){
		resp.setReturnCode(ReturnCode.DB_EXCEPTION);
		resp.setReturnMsg("数据库操作异常");
		resp.setTimestamp(resp.getTimestamp());
	}catch(Exception e){
		resp.setReturnCode(ReturnCode.OTHER_ERROR);
		resp.setReturnMsg("系统错误");
		resp.setTimestamp(req.getTimestamp());
	}
	return resp;
}
/**
 * app端请求添加设备方法
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:25:00<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public ObjResponse<Dygt> add(HttpServletRequest request){
	DygtReq req=null;
	ObjResponse<Dygt> resp=new ObjResponse<Dygt>();
	try{
		req=getJsonToObj(request,DygtReq.class);
		if(req==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("请求为空错误");
			return resp;
		}
		if(req.getObj()==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(req.getSecretKey()==null){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥为空");
			return resp;
		}
		if(req.getTimestamp()==null){
			resp.setReturnCode(ReturnCode.OTHER_ERROR);
			resp.setReturnMsg("时间戳为空");
			return resp;
		}
		if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥错误");
			return resp;
		}
		if(StringUtils.isBlank(req.getToken())){
			resp.setReturnCode(ReturnCode.TOKEN_INVALID);
			resp.setReturnMsg("票据为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(!validateToken(req.getToken(), req.getUserId())){
        	resp.setReturnCode(ReturnCode.TOKEN_INVALID);
        	resp.setReturnMsg("票据无效");
        	resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        boolean isVip =  companyService.isVip(req.getObj().getCompanyid());
        if(!isVip){
            int companyTowers = service.getCompanyTowerCount(req.getObj().getCompanyid());
            if(companyTowers>=PwtzConstants.THE_TRIAL_TOWERS_COUNT){
                resp.setReturnCode(ReturnCode.OTHER_ERROR);
                resp.setReturnMsg("单位低压杆塔数超过限制");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
        }
		Dygt rtObj=null;
		int result=0;
		if(service.load(req.getObj().getDeviceid())!=null){
		    result=service.update(req.getObj());
		}else{
		    rtObj=service.add(req.getObj());
		}
		if(null !=rtObj||result>0){
		    rtObj=service.load(req.getObj().getDeviceid());
		    dictionaryAuditService.add(DeviceDictConstants.DYGT_TOWER_HEIGHT_DICT_ID, rtObj.getTowerheight()+"");
		    dictionaryAuditService.add(DeviceDictConstants.DYGT_MANUFACTURE_FACTORY_DICT_ID, req.getObj().getProductionfactory());
		    dictionaryAuditService.add(DeviceDictConstants.DYGT_MODEL_DICT_ID, req.getObj().getModel());
			resp.setReturnCode(ReturnCode.SUCCESS);
			resp.setReturnMsg("添加成功");
            rtObj.setLastoperatorid(req.getUserId());
            service.update(rtObj);
			resp.setObj(rtObj);
			resp.setTimestamp(req.getTimestamp());
			setLog(rtObj.getDeviceid(),rtObj.getCompanyid(),AbstractController.低压杆塔,AbstractController.add,"添加低压杆塔["+rtObj.getDevicename()+"]成功",req.getUserId());
		}else{
			resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
			resp.setReturnMsg("调用接口失败");
			resp.setTimestamp(req.getTimestamp());
		}
	}catch(PwtzDatabaseException e){
		resp.setReturnCode(ReturnCode.DB_EXCEPTION);
		resp.setReturnMsg("数据库操作异常");
		resp.setTimestamp(resp.getTimestamp());
	}catch(Exception e){
		resp.setReturnCode(ReturnCode.OTHER_ERROR);
		resp.setReturnMsg("系统错误");
		resp.setTimestamp(req.getTimestamp());
	}
	return resp;
}
/**
 * app端请求修改对应的设备
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:28:44<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value="/update",method=RequestMethod.POST)
@ResponseBody
public ObjResponse<Dygt> update(HttpServletRequest request){
	DygtReq req=null;
	ObjResponse<Dygt> resp=new ObjResponse<Dygt>();
	try{
		req=getJsonToObj(request,DygtReq.class);
		if(req==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("请求为空错误");
			return resp;
		}
		if(req.getObj()==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(req.getSecretKey()==null){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥为空");
			return resp;
		}
		if(req.getTimestamp()==null){
			resp.setReturnCode(ReturnCode.OTHER_ERROR);
			resp.setReturnMsg("时间戳为空");
			return resp;
		}
		if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥错误");
			return resp;
		}
		if(StringUtils.isBlank(req.getToken())){
			resp.setReturnCode(ReturnCode.TOKEN_INVALID);
			resp.setReturnMsg("票据为空");
			resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(!validateToken(req.getToken(), req.getUserId())){
        	resp.setReturnCode(ReturnCode.TOKEN_INVALID);
        	resp.setReturnMsg("票据无效");
        	resp.setTimestamp(req.getTimestamp());
            return resp;
        }
		int result=service.update(req.getObj());
		if(result>0){
		    Dygt rtObj=service.load(req.getObj().getDeviceid());
		    dictionaryAuditService.add(DeviceDictConstants.DYGT_TOWER_HEIGHT_DICT_ID, rtObj.getTowerheight()+"");
            dictionaryAuditService.add(DeviceDictConstants.DYGT_MANUFACTURE_FACTORY_DICT_ID, req.getObj().getProductionfactory());
            dictionaryAuditService.add(DeviceDictConstants.DYGT_MODEL_DICT_ID, req.getObj().getModel());
			resp.setReturnCode(ReturnCode.SUCCESS);
			resp.setReturnMsg("修改成功");
            rtObj.setLastoperatorid(req.getUserId());
            service.update(rtObj);
			resp.setObj(rtObj);
			resp.setTimestamp(req.getTimestamp());
            setLog(rtObj.getDeviceid(),rtObj.getCompanyid(),AbstractController.低压杆塔,AbstractController.update,"低压杆塔修改["+rtObj.getDevicename()+"]成功",req.getUserId());
		}else{
			resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
			resp.setReturnMsg("调用接口失败");
			resp.setTimestamp(req.getTimestamp());
		}
	}catch(PwtzDatabaseException e){
		resp.setReturnCode(ReturnCode.DB_EXCEPTION);
		resp.setReturnMsg("数据库操作异常");
		resp.setTimestamp(resp.getTimestamp());
	}catch(Exception e){
		resp.setReturnCode(ReturnCode.OTHER_ERROR);
		resp.setReturnMsg("系统错误");
		resp.setTimestamp(req.getTimestamp());
	}
	return resp;
}
/**
 * app端请求删除设备id对应的设备
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:27:52<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public ObjResponse<Dygt> delete(HttpServletRequest request){
	DygtIdReq req=null;
	ObjResponse<Dygt> resp=new ObjResponse<Dygt>();
	try{
		req=getJsonToObj(request,DygtIdReq.class);
		if(req==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("请求为空错误");
			return resp;
		}
		if(req.getDeviceId()==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(req.getSecretKey()==null){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥为空");
			return resp;
		}
		if(req.getTimestamp()==null){
			resp.setReturnCode(ReturnCode.OTHER_ERROR);
			resp.setReturnMsg("时间戳为空");
			return resp;
		}
		if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥错误");
			return resp;
		}
		if(StringUtils.isBlank(req.getToken())){
			resp.setReturnCode(ReturnCode.TOKEN_INVALID);
			resp.setReturnMsg("票据为空");
			resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(!validateToken(req.getToken(), req.getUserId())){
        	resp.setReturnCode(ReturnCode.TOKEN_INVALID);
        	resp.setReturnMsg("票据无效");
        	resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        String deviceid=req.getDeviceId();
        Dygt rtObj=service.load(deviceid);
        boolean flag=service.deleteDevice(deviceid);
        if(flag){
            int result=service.delete(deviceid);
            if(result>0){
                rtObj=service.load(deviceid);
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("删除成功");
                rtObj.setLastoperatorid(req.getUserId());
                service.update(rtObj);
                resp.setTimestamp(req.getTimestamp());
                resp.setObj(rtObj);
                setLog(rtObj.getDeviceid(),rtObj.getCompanyid(),AbstractController.低压杆塔,AbstractController.delete,"删除低压杆塔["+rtObj.getDevicename()+"]成功",req.getUserId());
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("调用接口失败");
                resp.setTimestamp(req.getTimestamp());
            }
        }else{
            resp.setReturnCode(ReturnCode.ERROR_DELETE_REF_DEVICE);
            resp.setReturnMsg("删除相关杆塔失败");
            resp.setTimestamp(req.getTimestamp()); 
        }
		
	}catch(PwtzDatabaseException e){
		resp.setReturnCode(ReturnCode.DB_EXCEPTION);
		resp.setReturnMsg("数据库操作异常");
		resp.setTimestamp(resp.getTimestamp());
	}catch(Exception e){
		resp.setReturnCode(ReturnCode.OTHER_ERROR);
		resp.setReturnMsg("系统错误");
		resp.setTimestamp(req.getTimestamp());
	}
	return resp;
}
/**
 * app端请求查询设备id对应的设备信息
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:29:47<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value="/load",method=RequestMethod.POST)
@ResponseBody
public ObjResponse<Dygt> getObj(HttpServletRequest request){
	DygtIdReq req=null;
	ObjResponse<Dygt> resp=new ObjResponse<Dygt>();
	try{
		req=getJsonToObj(request,DygtIdReq.class);
		if(req==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("请求为空错误");
			return resp;
		}
		if(req.getDeviceId()==null){
			resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
			resp.setReturnMsg("参数错误");
			return resp;
		}
		if(req.getSecretKey()==null){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥为空");
			return resp;
		}
		if(req.getTimestamp()==null){
			resp.setReturnCode(ReturnCode.OTHER_ERROR);
			resp.setReturnMsg("时间戳为空");
			return resp;
		}
		if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
			resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
			resp.setReturnMsg("秘钥错误");
			return resp;
		}
		if(StringUtils.isBlank(req.getToken())){
			resp.setReturnCode(ReturnCode.TOKEN_INVALID);
			resp.setReturnMsg("票据为空");
			resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if(!validateToken(req.getToken(), req.getUserId())){
        	resp.setReturnCode(ReturnCode.TOKEN_INVALID);
        	resp.setReturnMsg("票据无效");
        	resp.setTimestamp(req.getTimestamp());
            return resp;
        }
		Dygt rtObj=service.load(req.getDeviceId());
		if(null !=rtObj){
			resp.setReturnCode(ReturnCode.SUCCESS);
			resp.setReturnMsg("查询成功");
			resp.setObj(rtObj);
			resp.setTimestamp(req.getTimestamp());
		}else{
			resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
			resp.setReturnMsg("调用接口失败");
			resp.setTimestamp(req.getTimestamp());
		}
	}catch(PwtzDatabaseException e){
		resp.setReturnCode(ReturnCode.DB_EXCEPTION);
		resp.setReturnMsg("数据库操作异常");
		resp.setTimestamp(resp.getTimestamp());
	}catch(Exception e){
		resp.setReturnCode(ReturnCode.OTHER_ERROR);
		resp.setReturnMsg("系统错误");
		resp.setTimestamp(req.getTimestamp());
	}
	return resp;
}
}
